<template>
  <div class="app-container">
    <!-- Filter -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="用例名称" style="width: 200px;" class="filter-item" />
      <el-select v-model="listQuery.project" filterable placeholder="请选择" class="filter-item" value="">
        <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button type="primary" class="filter-item" @click="handleFilter">查询</el-button>
      <el-button type="primary" style="margin-left: 20px" class="filter-item" @click="handleCreate">新增</el-button>
    </div>
    <!-- Table -->
    <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row="">
      <el-table-column label="ID" prop="id" width="80px">
      </el-table-column>
      <el-table-column label="用例标题" prop="case_num"></el-table-column>
      <el-table-column label="用例标题" prop="name">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" @click="handleUpdate(scope.row.id)">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="add_time">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.add_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" prop="edit_time">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.edit_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" style="width: 120px">
        <template slot-scope="{row}">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="handleUpdate(row.id)">编辑</el-button>
          <router-link :to="'/TestManager/step/list/'+ row.id">
            <el-button type="primary" size="small">操作步骤</el-button>
          </router-link>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" />
    <!-- 新增/编辑dialog -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用例编号" prop="case_num">
          <el-input v-model="temp.case_num" type="text" maxlength="30" minlength="3" show-word-limit/>
        </el-form-item>
        <el-form-item label="用例标题" prop="name">
          <el-input v-model="temp.name" type="text" maxlength="30" minlength="3" show-word-limit/>
        </el-form-item>
        <el-form-item label="用例类型" prop="type">
          <el-select v-model="temp.type" class="filter-item" placeholder="请选择">
            <el-option v-for="item in typeOptions" :key="item.index" :label="item.value" :value="item.index" />
          </el-select>
        </el-form-item>
        <el-form-item label="用例级别" prop="level">
          <el-select v-model="temp.level" class="filter-item" placeholder="请选择">
            <el-option v-for="item in levelOptions" :key="item.index" :label="item.value" :value="item.index" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="detail">
          <el-input v-model="temp.detail" type="textarea" maxlength="100" minlength="0" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCaseList, createCase, updateCase, deleteCase, getCase } from '@/api/case'
import { getProjectList } from '@/api/project'
import Pagination from '@/components/Pagination/index'
import { dateFormat } from '@/utils/data'
export default {
  name: 'List',
  components: { Pagination },
  data() {
    return {
      list: [],
      projectList: [],
      total: 0,
      loading: false,
      listQuery: {
        name: '',
        project: '',
        page: 1,
        limit: 20
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        id: undefined,
        case_num: '',
        name: '',
        type: 'web',
        level: 'low',
        detail: ''
      },
      typeOptions: [
        {
          index: 'web',
          value: 'WEB自动化'
        },
        {
          index: 'app',
          value: 'APP自动化'
        },
        {
          index: 'interface',
          value: '接口自动化'
        }
      ],
      levelOptions: [
        {
          index: 'low',
          value: '低'
        },
        {
          index: 'medium',
          value: '中'
        },
        {
          index: 'high',
          value: '高'
        }
      ],
      rules: {
        case_num: [{ required: true, message: '用例编号不能为空', trigger: 'change' }],
        name: [{ required: true, message: '用例编号不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
    this.getProjectList()
  },
  methods: {
    dateFormat: function(attr) {
      return dateFormat(attr)
    },
    getList() {
      this.loading = true
      getCaseList(this.listQuery).then((response) => {
        this.list = response.results
        this.total = response.count
        this.loading = false
      })
    },
    getProjectList() {
      const params = {
        page: 1,
        limit: 30
      }
      getProjectList(params).then((response) => {
        this.projectList = response.results
      })
    },
    resetForm() {
      this.temp = {
        id: undefined,
        case_num: '',
        name: '',
        type: 'web',
        level: 'low',
        detail: ''
      }
    },
    handleFilter() {
      this.getList()
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(id) {
      getCase(id).then((response) => {
        this.temp = response.data
      })
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      this.loading = true
      deleteCase(row.id).then(() => {
        this.handleFilter()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createCase(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: 'success',
              message: '保存成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempDate = Object.assign({}, this.temp)
          console.log(tempDate)
          updateCase(tempDate.id, tempDate).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'success',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
